package com.cyx.common.core.domain;

import com.cyx.common.core.enums.ResultCode;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class R<T> {

    // 消息状态码
    private int code;

    // 消息
    private String msg;

    // 数据对象
    private T data; // 请求某个接口返回的数据 需要定义成泛型

    public static <T> R<T> ok() {
        return assembleResult(ResultCode.SUCCESS, null);
    }

    public static <T> R<T> ok(T data) {
        return assembleResult(ResultCode.SUCCESS, data);
    }

    public static <T> R<T> error() {
        return assembleResult(ResultCode.ERROR, null);
    }

    public static <T> R<T> fail() {
        return assembleResult(ResultCode.FAILED, null);
    }

    public static <T> R<T> fail(ResultCode resultCode) {
        return assembleResult(resultCode, null);
    }

    public static <T> R<T> assembleResult(ResultCode resultCode, T data) {
        R<T> r = new R<T>();
        r.setCode(resultCode.getCode());
        r.setMsg(resultCode.getMsg());
        r.setData(data);
        return r;
    }

}
